package com.jeady.jxcompose

import android.content.Context
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.os.Environment
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.ui.Modifier
import androidx.core.graphics.drawable.toBitmap
import com.jeady.jxcompose.Utils.showToast
import java.io.File
import java.io.FileOutputStream


object ClickEffect {
    @OptIn(ExperimentalFoundationApi::class)
    fun Modifier.longClickSaveToDCIM(context: Context, id: Int): Modifier = then(
        combinedClickable(onLongClick = {
            val filePath = "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)}/timetodo/" +
                    "timetodo-${System.currentTimeMillis()}.jpg" // _${LocalDateTime.now()}

            if(File(filePath).parentFile?.exists()==false){
                File(filePath).parentFile?.mkdirs()
            }
//            if(!File(filePath).exists()){
//                File(filePath).createNewFile()
//            }
            context.getDrawable(id)?.toBitmap()?.compress(
                Bitmap.CompressFormat.JPEG,
                100,
                FileOutputStream(filePath)
            )
//            sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))
            MediaScannerConnection.scanFile(context, arrayOf(filePath), null, null)
            showToast(context,"保存成功")
        }){

        })
}